package agent.dynamic;

import com.sun.tools.attach.VirtualMachine;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * 模拟主程序，加载动态代理jar包
 */
public class Test {
    public static void main(String[] args) throws  Exception {
        System.out.println("主程序执行。。。。。");
        //获取进程列表，让用户自己选择连接哪个PID
        //执行jps指令
        Process process = Runtime.getRuntime().exec("jps");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        try (bufferedReader) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        }
        //用户输入进程ID
        Scanner scanner = new Scanner(System.in);
        String pid = scanner.next();
        //连接用户输入的进程
        VirtualMachine vm = VirtualMachine.attach(pid);

        //执行Java Agent的里的agentmain方法
        vm.loadAgent("D:/ideahome/all-demo/java-agent-demo/test/agent-dynamic-1.0.jar");
    }
}
